#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

#include "drv_db.h"

#define TABLE_NAME "test"
sqlite3 *db;

db_column_definition_t col_def[] = {
	{
		.cons = FEAT_NOT_NULL,
		.col_type = {
			.column_name = "DATE",
			.data_type = SQLITE_TEXT,
			.value.text_value = "'2025-02-22 12:01:02'",
		},
	},
	{
		.cons = FEAT_NOT_NULL,
		.col_type = {
			.column_name = "SN",
			.data_type = SQLITE_TEXT,
			.value.text_value = "'1700000000'",
		},
	},
	{
		.cons = FEAT_NOT_NULL,
		.col_type = {
			.column_name = "TYPE",
			.data_type = SQLITE_TEXT,
			.value.text_value = "'NULL'",
		},
	},
	{
		.cons = FEAT_NOT_NULL,
		.col_type = {
			.column_name = "DATA",
			.data_type = SQLITE_FLOAT,
			.value.float_value = 0.0,
		},
	},
};

// 定义用户表结构
db_create_table_params_t create_table_param = {
	.col_def = col_def,
	.col_count = sizeof(col_def) / sizeof(db_column_definition_t),
};

static void create_table_demo(void)
{
	// 创建表
	char* err_msg = NULL;
	if (drv_db_create_table(db, TABLE_NAME, &create_table_param) != SQLITE_OK)
	{
		DB_DB_LOG_ERROR("表 %s 创建失败\n", TABLE_NAME);
	}
	else
	{
		printf("表 %s 创建成功\n", TABLE_NAME);
	}
}

static void delete_table_demo(void)
{
	char* err_msg = NULL;
	if (drv_db_delete_table(db, TABLE_NAME) != SQLITE_OK)
	{
		DB_DB_LOG_ERROR("表 %s 删除失败\n", TABLE_NAME);
	}
	else 
	{
		printf("表 %s 删除成功\n", TABLE_NAME);
	}
}

static void find_table_demo(void)
{
	db_create_table_params_t* info = drv_db_find_table(db, TABLE_NAME);

	if(info->col_count == 0)
	{
		DB_DB_LOG_ERROR("NO THIS TABLE :%s", TABLE_NAME);
	}
	else
	{
		drv_db_printf_table_info(info);
	}

	drv_db_free_table_info(info);
}



static void insert_col(void)
{
	db_column_value_t col_data1[] = {
		{"DATE",	SQLITE_TEXT,	.value.text_value = "2025-02-24 10:22"},
		{"SN",		SQLITE_TEXT,	.value.text_value = "1712480001"},
		{"TYPE",	SQLITE_TEXT,	.value.text_value = "CO2"},
		{"DATA",	SQLITE_FLOAT,	.value.float_value = 1.2},
	};

	db_column_value_t col_data2[] = {
		{"DATE",	SQLITE_TEXT,	.value.text_value = "2025-02-24 10:22"},
		{"SN",		SQLITE_TEXT,	.value.text_value = "1712481111"},
		{"TYPE",	SQLITE_TEXT,	.value.text_value = "CO2"},
		{"DATA",	SQLITE_FLOAT,	.value.float_value = 3.3},
	};

	db_line_col_t line_data[] = {
		{
			.col = col_data1,
			.col_count = sizeof(col_data1) / sizeof(db_column_value_t),
		},
		{
			.col = col_data2,
			.col_count = sizeof(col_data1) / sizeof(db_column_value_t),
		},
	};

	db_col_insert_param_t data = {
		.insert_data = line_data,
		.data_count = sizeof(line_data) / sizeof(db_line_col_t),
	};

	// 在事务中执行批量插入
	db_col_insert_data(db, TABLE_NAME, data);
}

static void delete_col(void)
{
	/*SN = 1712480000 AND TYPE = CO2*/
	db_where_condition_t cond[] =
	{
		{
			.value = {
				.column_name = "SN",
				.value = {
					.text_value = "1712481234",
				},
				.data_type = SQLITE_TEXT,
			},
			.cmp_op = CMP_EQ,
			.is_not = false,
			.logic_op = LOGIC_NONE,
		},
		// {
		// 	.value = {
		// 		.column_name = "SN",
		// 		.value = {
		// 			.text_value = "1712481111",
		// 		},
		// 		.data_type = SQLITE_TEXT,
		// 	},
		// 	.cmp_op = CMP_EQ,
		// 	.is_not = false,
		// 	.logic_op = LOGIC_OR,
		// },
	};

	db_col_delete_param_t param = {
		.param = cond,
		.param_count = sizeof(cond) / sizeof(db_where_condition_t),
	};

	db_col_delete_data(db, TABLE_NAME, param);
}

static void update_col(void)
{
	/*SN = 1712481111 AND TYPE = CO2 -> SN = 1712481234 DATA = 12.34*/
	// 构造更新参数
	db_column_value_t set_columns[] = {
		{"SN",		SQLITE_TEXT,	.value.text_value = "1712481234"},
		{"DATA",	SQLITE_FLOAT,	.value.float_value = 4.4},
	};

	db_where_condition_t conditions[] = {
		{"SN",		SQLITE_TEXT, CMP_EQ, LOGIC_NONE,	false, .value.value.text_value = "1712481111"},
		{"TYPE",	SQLITE_TEXT, CMP_EQ, LOGIC_AND,		false, .value.value.text_value = "CO2"},
	};

	db_update_param param = {
		.set_columns = set_columns,
		.set_count = 2,
		.conditions = conditions,
		.condition_count = 2
	};

	db_col_update_data(db, TABLE_NAME, param);
}

static void select_col(void)
{
	char* col_name[] = {
		"SN",
		"DATE",
		"TYPE",
		"DATA",
	};

	db_where_condition_t cond[] =
	{
		{
			.value = {
				.column_name = "SN",
				.value = {
					.text_value = "1712481111",
				},
				.data_type = SQLITE_TEXT,
			},
			.cmp_op = CMP_EQ,
			.is_not = false,
			.logic_op = LOGIC_NONE,
		},
	};


	db_select_param param = {
		.select_columns = (const char**)col_name,
		.column_count = 4,
		.conditions = NULL,
		.condition_count = 0,
	};

	db_result_queue* all_data = db_col_query_data(db, TABLE_NAME, param);

	if(all_data == NULL)
	{
		printf("error\r\n");
	}

	db_result_node* node = all_data->head;
	
	while(node != NULL)
	{
		printf_node_info(node);
		node = node->next;
	}

	db_col_select_res_free(all_data);
}

int main(int argc, char* argv[])
{

	/* 打开数据库 */
	db = drv_db_open_db(argv[1]);
	if(db == NULL)
	{
		printf("open db error\r\n");
		goto CLOSE_DB;
	}

	// create_table_demo();
	find_table_demo();
	// delete_table_demo();
	// find_table_demo();


	insert_col();

	delete_col();

	update_col();

	select_col();

CLOSE_DB:
	/* 关闭数据库 */
	drv_db_close_db(db);

	return 0;
}
